﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using THSERP.BaseProvider;
using THSLib;
using THSERP.BusinessEntities;
using System.Security.Cryptography;
namespace THSERP.Modules.Handover
{
    public partial class guiCompleteHandover : THSERPScreen
    {
        public guiCompleteHandover()
        {
            InitializeComponent();
        }

        private void fld_btnConfirm_Click(object sender, EventArgs e)
        {
            if (fld_txtPassword.Text == "" || fld_txtUserName.Text == "")
            {
                MessageBox.Show("Chưa nhập thông tin!","Cảnh báo",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                return;
            }
            if (IsUserReceive(fld_txtUserName.Text.Trim()))
            {
                if (IsAuthenticated(fld_txtUserName.Text, fld_txtPassword.Text))
                {
                    this.DialogResult = DialogResult.OK;
                    this.Dispose();
                }
                else
                {
                    MessageBox.Show("Người dùng hoặc mật khẩu không chính xác. Vui lòng kiểm tra lại!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            else
            {
                MessageBox.Show("Người dùng không trùng với nhân viên nhận ca!", "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

        }
        public bool IsAuthenticated(String strUserName, String strPassword)
        {
            ADUsersController objADUsersController = new ADUsersController();
            ADUsersInfo objADUsersInfo = new ADUsersInfo();
            objADUsersInfo = (ADUsersInfo)objADUsersController.GetObjectByName(strUserName.ToLower());
            if (objADUsersInfo != null)
            {
                String _encodedPassword = Convert.ToBase64String(SHA1Managed.Create().ComputeHash(ASCIIEncoding.ASCII.GetBytes(strPassword)));
                if (_encodedPassword.Equals(objADUsersInfo.ADPassword))
                    return true;

            }
            return false;
        }

        public bool IsUserReceive(String strUserName)
        {
            //Get iADUserID
            bool isUserReceive=false;
           HandoverEntities entity= (HandoverEntities)((BaseModuleERP)this.Module).CurrentModuleEntity;
            ADUsersController objADUsersController = new ADUsersController();
            ADUsersInfo objADUsersInfo = (ADUsersInfo)objADUsersController.GetObjectByName(strUserName);
            if(objADUsersInfo!=null)
            {
                if (objADUsersInfo.FK_HREmployeeID == ((BRHandoversInfo)entity.MainObject).FK_HREmployeeInID)
                    isUserReceive = true;
            }
            return isUserReceive;
        }

        public void SetCurrentUserLogin(string strUserName)
        {
            ADUsersController objADUsersController = new ADUsersController();
            ADUsersInfo objADUsersInfo = new ADUsersInfo();
            HandoverEntities entity = (HandoverEntities)((BaseModuleERP)this.Module).CurrentModuleEntity;
            objADUsersInfo = (ADUsersInfo)objADUsersController.GetObjectByName(strUserName);
            if (objADUsersInfo != null)
            {
                THSApp.CurrentUser = objADUsersInfo.ADUserName;
                THSApp.CurrentWorkingShift = (HRShiftsInfo)new HRShiftsController().GetObjectByID(((BRHandoversInfo)entity.MainObject).FK_HRShiftInID);
                GELanguagesController objLanguageController = new GELanguagesController();
                GELanguagesInfo objLanguageInfo = new GELanguagesInfo();
                int iLanguageID = ((ADUserGroupsInfo)new ADUserGroupsController().GetObjectByID(objADUsersController.GetUserGroupOfUser(objADUsersInfo.ADUserID))).ADLanguageIDCombo;
                objLanguageInfo = (GELanguagesInfo)objLanguageController.GetObjectByID(iLanguageID);
                if (objLanguageInfo != null)
                    THSApp.CurrentLang = objLanguageInfo.GELanguageName.Trim();
            }
        }

        private void thsButton1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void fld_txtPassword_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                fld_btnConfirm_Click(new object(), new EventArgs());
            }
        }
    }
}